﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Surface.Core;
using Microsoft.Surface.Core.Manipulations;

namespace Sublimation
{
    public class EnergyRingSurf:EnergyRing
    {
        Contact controler= null;
        EnergyRingManipulator manipulator;
        public EnergyRingSurf(WorldPropertiesSurf worldproperties, Tower owner):base(worldproperties,owner){
            manipulator = new EnergyRingManipulator(position, worldproperties, this);
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            ReadOnlyContactCollection currentContacts = ((WorldPropertiesSurf)worldProperties).Contacts;
            if (controler != null)
            {
                if (currentContacts.Contains(controler.Id))
                {
                    controler = currentContacts.GetContactFromId(controler.Id);
                    rotation = 0;
                    position.X = controler.CenterX + 15;
                    position.Y = controler.CenterY - 15;
                }
                else
                {
                    ((WorldPropertiesSurf)worldProperties).Owned.Remove(controler.Id);
                    controler = null;
                    mouving = false;
                }
            }
            else
            {  
                foreach (Contact contact in currentContacts)
                {
                    if (contact.IsFingerRecognized && belongs(contact) && !((WorldPropertiesSurf)worldProperties).Owned.Contains(contact.Id))
                    {
                        controler = contact;
                        ((WorldPropertiesSurf)worldProperties).Owned.Add(contact.Id);
                        mouving = true;
                        break;
                    }
                }
            }
        }

        public bool belongs(Contact contact)
        {
            Rectangle zone1 = new Rectangle((int)position.X, (int)position.Y, 30, 30);
            Rectangle zone2 = new Rectangle((int)contact.X, (int)contact.Y, (int)contact.Bounds.Width, (int)contact.Bounds.Height);
            return zone1.Intersects(zone2);
        }
    }
}
